/////////////////////////登录
var tohrefurl="weixin/home.html";//原始跳转页面
function fromesharepage(myid){
	//如果有 fromeuser 转发注册页面跳转进来
	if($.cookie("fromuser")){
		//唯一途径从注册页进入 获取注册页传递的cookie数据
		var loca=$.cookie("fromid");

		if(/newsid=/.test(loca)){
			var newsid=loca.match(/newsid=[0-9]+/)[0].match(/[0-9]+/)[0];
			tohrefurl="newsdetail.html?newsid="+newsid+"&fromuser="+myid;
		}else if(/qid=/.test(loca)){
			var qid=loca.match(/qid=[0-9]+/)[0].match(/[0-9]+/)[0];
			tohrefurl="questiondetail.html?qid="+qid+"&fromuser="+myid;
		}else if(/eid=/.test(loca)){
			var eid=loca.match(/eid=[0-9]+/)[0].match(/[0-9]+/)[0];
			tohrefurl="shareexppage.html?eid="+eid+"&fromuser="+myid;
		}
	}
}
if(localStorage.getItem("mp")){
    //console.log("have phonenumber:"+localStorage.getItem("mp"));
    $("#phonenumber").val(localStorage.getItem("mp"));
    $("#password").val(localStorage.getItem("pw"));
    //$("#password").focus();
    //自动登录
     //不是退出
    if($.cookie("issignout")!=="退出登录"){
        $.cookie("MYID",hex_md5(localStorage.getItem("mp")).substring(7, 23));
		//转发页面进来 自动登录修改跳转地址
		fromesharepage($.cookie("MYID"));
        location.href=tohrefurl;
    }
}else{
    console.log("have no phonenumber");
    $("#phonenumber").focus();
}
if($.cookie("mp")){
    $("#phonenumber").val($.cookie("mp"));
    $("#password").val($.cookie("pw"));
}
function login(){
	if(!$("#phonenumber").val().match(/^1[34578]\d{9}$/)){
		$("#tishiinfor").html("手机号不正确");
	}else{
		var phonenumber=$("#phonenumber").val();
		var password=$("#password").val();
		var md5phonenumber=hex_md5(phonenumber).substring(7, 23);
		var md5password=hex_md5(password).substring(7, 23);
		//$("#tishiinfor").html("正在登录...");
	//环信
		//创建链接
		var conn = new WebIM.connection({
			https: WebIM.config.https,
			url: WebIM.config.xmppURL,
			isAutoLogin: WebIM.config.isAutoLogin,
			isMultiLoginSessions: WebIM.config.isMultiLoginSessions
		});
		//登录
		var options = {
			apiUrl: WebIM.config.apiURL,
			user: md5phonenumber,
			pwd: md5phonenumber,
			appKey: "pulu#pulu"
		};
		conn.open(options);
		conn.listen({
			onOpened: function ( message ) {          //连接成功回调
				//如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
				conn.setPresence();
				console.log("连接成功");
			},
			onRoster: function ( message ) {
				//console.log(message);
			}         //处理好友申请
		});
		//转发页面进入 添加好友
		function shareinaddfri(){
			//如果有 fromeuser 转发注册页面跳转进来  加好友
			if($.cookie("fromuser")){
				//获取我的昵称
				var thisnick;
				$.ajax({
					type: "post",
					url: "http://42.62.66.198:9001/api/puluuser/getuser",
					data: { "userId":md5phonenumber},
					datatype: "json",
					success:function(d){
						thisnick=d.Data.nick;
						//获取好友列表判断是不是好友
						$.ajax({
							datatype:"json",
							type:"get",
							url:"http://42.62.66.198:9001/api/PuluGoodFriend/GetByUserId?userId="+md5phonenumber,
							success:function(d){
								//console.log("别人的好友列表");
								console.log(d);
								var isfriend="yes";
								if(d.Data.length>0){
									for(var i=0;i<d.Data.length;i++){
										//已经加好友了
										if(d.Data[i].friendId===$.cookie("fromuser")){console.log("已经是好友了");}
										else{isfriend="no";console.log("还不是好友");}
									}
									if(isfriend==="yes"){
										//环信 发送好友请求
										conn.subscribe({
											to: $.cookie("fromuser"),
											message:"我是"+thisnick
										});
									}
								}else{
									//环信 发送好友请求
									conn.subscribe({
										to: $.cookie("fromuser"),
										message:"我是"+thisnick
									});
								}

							}
						});
					}
				})
			}else{
				location.href=tohrefurl;
			}
		}
		//服务器验证
		$.ajax({
			type:"post",
			url:"http://42.62.66.198:9001/api/PuluUser/Login",
			data:{"userId":md5phonenumber,"password":md5password},
			success:function(d){

				//location.href="home.html";
				if(d.Message==="密码错误"){
					$("#tishiinfor").html("密码错误");
				}else if(d.Message==="手机号未注册"){
					$("#tishiinfor").html("手机号未注册");
				}else if(d.Message==="正常登录"){
					$.cookie("MYNICK", d.Data.userNick);
					$.cookie("MYID",d.Data.userId);
					$.cookie("MYAVA", d.Data.userAva);
					fromesharepage(d.Data.userId);
					if($.cookie("wxopenid")){
						$.ajax({
							type:"post",
							url:"http://42.62.66.198:9001/api/WebChatInfo/AddWebChatInfo",
							data:{"userId":md5phonenumber,"webchatId":$.cookie("wxopenid")},
							success:function(d){
								shareinaddfri();
							}
						})
					}else{
						//如果有 fromeuser 转发注册页面跳转进来  加好友
						if($.cookie("fromuser")){
							shareinaddfri();
						}else{
							location.href=tohrefurl;
						}
					}
				}
			}
		});
	}
}
//点击
$("#denglu>div").click(function(){
	login();
});
//键盘
document.onkeydown=function(event){
	var e = event || window.event || arguments.callee.caller.arguments[0];
	if(e && e.keyCode==13){ // enter 键
		login();
	}
};
$('input').bind('input propertychange', function() {
    $("#tishiinfor").html("");
});
//忘记密码
$("#forgetpw").click(function(){
    location.href="forgetpw.html";
});